package _test;

import com.ruiyi.baseparser.Group;
import com.ruiyi.baseparser.ParserUtil;
import com.ruiyi.baseparser.RuiyiType;
import com.ruiyi.json.Json_Job;
import com.ruiyi.json.Json_JobModel;
import com.ruiyi.json.Json_Product;
import com.ruiyi.json.Json_Result;
import com.ruiyi.json.parser.JobModelParser;
import com.ruiyi.json.parser.RuiyiParsers;
import com.ruiyi.util.LogUtil;


public class Test {
	private static final String TAG = "Main\t\t";

	public static void main(String[] argz) {
//		testParseProduct();
//		
//		testParseProductList();

		testComplexContent();
	}

	static final String result = "{\"success\":\"false\",\"code\":-110107,\"message\":\"找不到产品\"}";

	static final String productcontent = "{\"producid\":1997,\"productname\":\"areful1997@163.com\"}";
	static final String productlistcontent = "[{\"producid\":321394,\"productname\":\"钱包\",\"siteid\":4714333,\"serialnumber\":\"\",\"summary\":\"\",\"content\":\"\",\"tag\":\"\",\"price\":0.0,\"discountprice\":0.0,\"displayorder\":6,\"createtime\":\"May 9, 2012 1:43:52 AM\",\"updatetime\":\"May 9, 2012 1:43:52 AM\",\"state\":0,\"cateid\":960481,\"image\":\"http://www.locoso.com/uploads/s/0000/0000/0047/ef5d/20120509134352220_{0}.jpeg\",\"createip\":\"222.92.17.34\",\"view\":0,\"mediaid\":126555,\"toppro\":0,\"updateip\":\"222.92.17.34\",\"catename\":\"默认\"},{\"producid\":321393,\"productname\":\"电脑包\",\"siteid\":4714333,\"serialnumber\":\"\",\"summary\":\"\",\"content\":\"\",\"tag\":\"\",\"price\":0.0,\"discountprice\":0.0,\"displayorder\":5,\"createtime\":\"May 9, 2012 1:41:17 AM\",\"updatetime\":\"May 9, 2012 1:41:17 AM\",\"state\":0,\"cateid\":960481,\"image\":\"http://www.locoso.com/uploads/s/0000/0000/0047/ef5d/20120509134117033_{0}.jpeg\",\"createip\":\"222.92.17.34\",\"view\":0,\"mediaid\":126554,\"toppro\":0,\"updateip\":\"222.92.17.34\",\"catename\":\"默认\"},{\"producid\":321392,\"productname\":\"拉杆箱\",\"siteid\":4714333,\"serialnumber\":\"\",\"summary\":\"\",\"content\":\"\",\"tag\":\"\",\"price\":0.0,\"discountprice\":0.0,\"displayorder\":4,\"createtime\":\"May 9, 2012 1:40:35 AM\",\"updatetime\":\"May 9, 2012 1:40:35 AM\",\"state\":0,\"cateid\":960481,\"image\":\"http://www.locoso.com/uploads/s/0000/0000/0047/ef5d/20120509134035216_{0}.jpeg\",\"createip\":\"222.92.17.34\",\"view\":0,\"mediaid\":126553,\"toppro\":0,\"updateip\":\"222.92.17.34\",\"catename\":\"默认\"}]";

	static final String complexcontent = "{\"companyid\":4303828,\"companyname\":\"hycdog\",\"joblist\":[{\"jobname\":\"招聘01\",\"city\":\"北京市辖区\",\"number\":0,\"workyear\":\"10\",\"education\":\"本科\",\"jobdesc\":\"清洁工\",\"cutoffdate\":\"2012-04-01\",\"salary\":\"面议\"},{\"jobname\":\"招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试01\",\"city\":\"北京市辖区\",\"number\":10,\"workyear\":\"2\",\"education\":\"本科\",\"jobdesc\":\"招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试招聘测试\",\"cutoffdate\":\"2012-06-30\",\"salary\":\"面议\"}]}";

	private static void testParseProduct() {
		// RuiyiType t = jsonUtil.ParseProductList(result);
		RuiyiType t = RuiyiParsers.ParseProduct(productcontent);
		// RuiyiType t = jsonUtil.ParseProductList(productlistcontent);

		if (t instanceof Json_Product) {
			LogUtil.e(TAG,
					"Product Name:\t" + ((Json_Product) t).getProductName());
			return;
		}

		if (t instanceof Json_Result) {
			LogUtil.e(TAG, "parse product failed, parse result success.");
			return;
		}

		LogUtil.e(TAG, "parse product failed.");
	}

	@SuppressWarnings("unchecked")
	private static void testParseProductList() {
		// RuiyiType t = jsonUtil.ParseProductList(result);
		// RuiyiType t = jsonUtil.ParseProduct(productcontent);
		RuiyiType t = RuiyiParsers.ParseProductList(productlistcontent);
		if (t instanceof Group<?>) {
			for (Json_Product p : ((Group<Json_Product>) t)) {
				LogUtil.e(TAG, "parse product list success");
				LogUtil.e(TAG, "Product Name:\t" + p.getProductName());
			}
			return;
		}

		if (t instanceof Json_Result) {
			LogUtil.e(TAG, "parse product list failed, parse result success.");
			return;
		}
		LogUtil.e(TAG, "parse product list failed.");
	}

	private static void testComplexContent() {
		try {
			Json_JobModel model = (Json_JobModel) ParserUtil.consume(
					new JobModelParser(), complexcontent);

			LogUtil.e(TAG, "id:" + model.getCompanyID());
			LogUtil.e(TAG, "name:" + model.getCompanyName() + "\n");

			for (Json_Job job : model.getJobList()) {
				LogUtil.e(TAG, "name:" + job.getJobName());
				LogUtil.e(TAG, "city:" + job.getCity());
				LogUtil.e(TAG, "numbers:" + job.getJobNumbers());
				LogUtil.e(TAG, "cutoffdate:" + job.getCutoffDate());
				LogUtil.e(TAG, "education:" + job.getEducation());
				LogUtil.e(TAG, "description:" + job.getJobDescription());
				LogUtil.e(TAG, "salary:" + job.getSalary());
				LogUtil.e(TAG, "workyear:" + job.getWorkYear());
			}
		} catch (Exception e) {
			LogUtil.e(TAG, "", e);
		}
	}
}